package yl.hs.bmipfull.service.chart.impl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.material.IPurchaseReceiptDao;
import yl.hs.bmipfull.daos.material.IRawMaterialInfoDao;
import yl.hs.bmipfull.daos.material.IRawMaterialInventoryDao;
import yl.hs.bmipfull.daos.material.IWarehouseDao;
import yl.hs.bmipfull.pojo.material.PurchaseReceiptIEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialInfoEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialInventoryEntity;
import yl.hs.bmipfull.pojo.material.WarehouseEntity;
import yl.hs.bmipfull.service.chart.IRawMaterialChartService;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.viewmodels.production.ProductionRecordModel;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 谢东
 * @apiNote
 * @since 2022-03-18 9:39 创建
 */
@Service
public class RawMaterialChartServiceImpl implements IRawMaterialChartService {

    @Autowired
    IWarehouseDao warehouseDao;
    @Autowired
    IProductionLineService productionLineService;
    @Autowired
    IPurchaseReceiptDao purchaseReceiptDao;
    @Autowired
    IRawMaterialInfoDao rawMaterialInfoDao;
    @Autowired
    private IRawMaterialInventoryDao rawMaterialInventoryDao;

    @Override
    public List<Object> getRealTimeInventory() throws Exception {
        List<Object> result=new ArrayList<>();
        var lineList=productionLineService.queryList("");
        for (var line:lineList ) {
            Map<String,Object> map=new HashMap<>();
            map.put("lineName",line.getPlName());
            var list=warehouseDao.queryPortListByLineSN("",line.getSn());
            List<WarehouseEntity> gl=new ArrayList<>();
            List<WarehouseEntity> fl=new ArrayList<>();
            for (var entity:list) {
                if (entity.getWhType()==0){
                    gl.add(entity);
                }else{
                    fl.add(entity);
                }
            }
            map.put("gl",gl);
            map.put("fl",fl);
            result.add(map);
        }
        return result;
    }

    @Override
    public Map<String, Object> getPurchaseByMonth() {
        try {
        Map<String,Object> map = new HashMap<>();
        var listentity = purchaseReceiptDao.getPurchaseByMonth();
        var rawtnfo = rawMaterialInfoDao.queryList(null);
        List<Integer> month = new ArrayList<Integer>();
        List<Object> number = new ArrayList<>();
        month.add(1);month.add(2);month.add(3);month.add(4);month.add(5);month.add(6);month.add(7);month.add(8);month.add(9);month.add(10);month.add(11);month.add(12);
        map.put("legend",rawtnfo.stream().map(e -> e.getRmSpec()).collect(Collectors.toList()));
        map.put("xAxis",month);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        var currentYear = cal.get(Calendar.YEAR);
        List<Object> data = new ArrayList<>();
        rawtnfo.forEach(s->{
            List<BigDecimal> shu = new ArrayList<>();
              for(int i=1;i<=12;i++){
                  BigDecimal result = BigDecimal.valueOf(Double.valueOf(0));
                  cal.set(currentYear,i-1,1);
                  var startTime =   cal.getTime();
                  cal.set(currentYear,i,1);
                  var endTime = cal.getTime();
                  val purchaseReceiptEntityStream = listentity.stream().filter(d -> d.getRmSN().equals(s.getSn()) && d.getTime().before(endTime) && d.getTime().after(startTime)).toList();
                  if(purchaseReceiptEntityStream.size()>0){
                      for (PurchaseReceiptIEntity purchaseReceiptEntity : purchaseReceiptEntityStream) {
                          result = result.add(purchaseReceiptEntity.getPrice());
                      }
                      result = result.divide(BigDecimal.valueOf(purchaseReceiptEntityStream.size()));
                      shu.add(result);
                  }else{
                      shu.add(result);
                  }

              }
            data.add(shu);
        });
        map.put("data",data);
            return map;
        }catch (Exception ee){
            return null;
        }

    }

    @Override
    public Map<String, List<Object>> materialInventory() {
        Map<String, List<Object>> result = Maps.newHashMap();
        List<Object> xAxis = Lists.newArrayList();
        List<Object> data = Lists.newArrayList();
        List<RawMaterialInfoEntity> infos = rawMaterialInfoDao.getAllInfo();
        var inventory =  rawMaterialInventoryDao.queryList(null).stream().filter(s-> s.getQuantity().compareTo(new BigDecimal(1))>0).sorted(Comparator.comparing(RawMaterialInventoryEntity::getQuantity).reversed()).collect(Collectors.toList());
        for (RawMaterialInventoryEntity info : inventory) {
            xAxis.add(info.getRmCateName() + "-" + info.getRmName() + "-" + info.getRmSpec());
            data.add(info.getQuantity());
        }
        result.put("xAxis", xAxis);
        result.put("data", data);
        return result;
    }

}
